#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Kang.Cunhua
# @Date:   2015-11-15 11:27:29
# @Last Modified by:   Kang.Cunhua
# @Last Modified time: 2015-11-15 13:33:21
from twisted.internet import protocol, reactor, endpoints
# 下面这个class是整个程序的核心


class Echo(protocol.Protocol):
    """docstring for Echo"""

    def __init__(self, arg):
        super(Echo, self).__init__()
        self.arg = arg

    def dataReceived(self, data):
        # 把请求过来的数据原封不动地发回去
        self.transport.write(data)
# 工厂模型


class EchoFactory(protocol.Factory):
    """docstring for EchoFactory"""

    def __init__(self, arg):
        super(EchoFactory, self).__init__()
        self.arg = arg

    def buildProtocol(self, addr):
        return Echo()

# 监听tcp端口1234
endpoints.serverFromString(reactor, "tcp:1234").listen(EchoFactory())
# 开启server
reactor.run()

# output：使用windows客户端cmd每一个字符都响应
# kkcchh ppcc

# output: 使用Xshell客户端
# Xshell 5 (Build 0446)
# Copyright (c) 2002-2014 NetSarang Computer, Inc. All rights reserved.

# Type `help' to learn how to use Xshell prompt.
# [C:\Users\Kang.Cunhua]$ telnet 127.0.0.1 1234


# Connecting to 127.0.0.1:1234...
# Connection established.
# To escape to local shell, press 'Ctrl+Alt+]'.
# kangcunhua
# kangcunhua
# wahaha
# wahaha
# pc
# pc

# Connection closed by foreign host.

# Type `help' to learn how to use Xshell prompt.
# [C:\Users\Kang.Cunhua]$
